/*
 * @(#)CommonHttpBO.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.hangame.mbl.bo.http;

import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException;
import org.json.JSONObject;

import com.hangame.mbl.util.HttpClientUtil;
import com.nhncorp.hancore.util.http.HttpClientSimple;
import com.nhncorp.hancore.util.xml.XMLDataMap;
import com.nhncorp.lucy.common.data.DataHandlerFactory;
import com.nhncorp.lucy.web.metadata.Inject;

// TODO: Auto-generated Javadoc
/**
 * The Class CommonHttpBO.
 * 
 * @author khpark
 */
public class CommonHttpBO {
	
	/** The log. */
	private static Log log = LogFactory.getLog(CommonHttpBO.class);

	/** The Constant URL_GETTING_MOBILE_NUM. */
	private static final String URL_GETTING_MOBILE_NUM = DataHandlerFactory.getDataHandler().get("url/mbl_getting_mobile_num");
	
	/** The Constant URL_GETTING_MOBILE_NUM_MID. */
	private static final String URL_GETTING_MOBILE_NUM_MID = DataHandlerFactory.getDataHandler().get("url/mbl_getting_mobile_num_mid");
	
	/** The http client context. */
	private HttpClientContext httpClientContext;

	/**
	 * Sets the http client context.
	 * 
	 * @param httpClientContext the new http client context
	 */
	@Inject(beanId = "httpClientContext")
	public void setHttpClientContext(HttpClientContext httpClientContext) {
		this.httpClientContext = httpClientContext;
	}

	/**
	 * 폰번호 가져오기.
	 * 
	 * @param userId the user id
	 * @return the mobile phone num
	 * @return
	 */
	@Deprecated
	public String getMobilePhoneNum(final String userId) {
		XMLDataMap map = this.httpClientContext.doGetXmlDataMapTemplate(new HttpClientStrategy() {
			@Override
			public HttpClientSimple makeHttpClient() {
				HttpClientSimple client = new HttpClientSimple(URL_GETTING_MOBILE_NUM);
				client.setParam("memberid", userId);
				client.setUsingKeepAlive(false);
				client.setUsingCache(false);
				client.setRequestCharset(Charset.forName("MS949")); // UTF8->MS949로변경
				return client;
			}
		});

		if (map == null) {
			return "";
		}

		String phoneNum = "";
		try {
			// TODO : 성공여부
//			String result = map.getAsText("RESULT"); //성공여부
			phoneNum = map.getAsText("MOBILE");
//			boolean isOwner = "Y".equals(map.getAsText("MOBILEOWNER"));
		} catch (Exception e) {
			log.error(e, e);
			return "";
		}

		return phoneNum;
	}

	/**
	 * Gets the mobile phone num from mid.
	 * 
	 * @param memberNo the member no
	 * @return the mobile phone num from mid
	 */
	public String getMobilePhoneNumFromMid(String memberNo) {
		String url = URL_GETTING_MOBILE_NUM_MID + "/" + memberNo;
		String result = "";
		try {
			String[] results = HttpClientUtil.getResponseBodyGet(url, "UTF-8", 1000);
			if (Integer.valueOf(results[0]) == 200) {
				result = results[1];
			}
		} catch (HttpException e) {
			log.error("get Mobile Number From MID :: ", e);
		}
		
		String phoneNum = "";
		if (StringUtils.isNotEmpty(result)) {
			try {
				JSONObject jObj = new JSONObject(result);
				phoneNum = jObj.getString("phoneNo");
			} catch (Exception e) {
				log.error(e, e);
				return "";
			}
		}

		return phoneNum;
	}
}
